<?php

$port='/dev/ttyS5';
$baudRate = [
    'baud'      => 9600,
    'bits'      => 8,
    'stop'      => 1,
    'parity'    => 0,
];
$fd = 1;
$fd = conn_com($fd,$port);
// $fd = dio_open($port, O_RDWR|O_NOCTTY|O_NONBLOCK);
// dio_tcsetattr($fd,$baudRate);

var_dump($fd);
$cmd = '2733';
while(true)
{
    dio_write($fd,bin2hex($cmd)); 
    $v = dio_read($fd, 0);
    var_dump($v);
    sleep(5);
}




















function conn_com($fd ,$com_name,$baud='9600',$data = 8,$stop = 1)
{
    if($fd) return $fd;

    if(stripos(php_uname(),'windows')>-1){
        try {
            @exec("mode $com_name: baud=$baud data=$data stop=$stop parity=n xon=on");
            $fd = dio_open("$com_name:",O_RDWR);
        } catch(Exception $e) {
            echo "$com_name conn field \r\n";
            return false;
        }
        return $fd;
    }else{
        try{
            $fd = @dio_open($com_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
            if(!$fd) return false;
            //dio_fcntl($fd, F_SETFL, O_SYNC);
            dio_tcsetattr($fd, array(
                'baud' => $baud,
                'bits' => 8,
                'stop'  => 1,
                'parity' => 0
            ));
        } catch(Exception $e){
            echo "$com_name conn field \r\n";
            return false;
        }
    }

    echo "$com_name success\r\n";
    return $fd;
}